package com.quick.system.service.impl;

import com.quick.common.constants.CacheConstants;
import com.quick.common.constants.Constants;
import com.quick.common.exception.ServiceException;
import com.quick.common.redis.RedisCache;
import com.quick.common.utils.SecurityUtils;
import com.quick.common.utils.StringUtils;
import com.quick.system.domain.SysConfig;
import com.quick.system.mapper.SysConfigMapper;
import com.quick.system.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 参数配置 服务层实现
 *
 * @Author: Administrator
 * @Date: 2023/7/17
 */
@Service
public class SysConfigServiceImpl implements SysConfigService {
    @Autowired
    private SysConfigMapper sysConfigMapper;
    @Autowired
    private RedisCache redisCache;


    /**
     * 查询参数配置信息
     *
     * @param configId 参数配置ID
     * @return 参数配置信息
     */
    @Override
    public SysConfig selectConfigById(Long configId) {
        return sysConfigMapper.selectConfigById(configId);
    }

    /**
     * 查询参数配置列表
     *
     * @param config 参数配置信息
     * @return 参数配置集合
     */
    @Override
    public List<SysConfig> selectConfigList(SysConfig config) {
        return sysConfigMapper.selectConfigList(config);
    }

    /**
     * 新增参数配置
     *
     * @param config 参数配置信息
     * @return 结果
     */
    @Override
    public int insertConfig(SysConfig config) {
        config.setCreateTime(new Date());
        config.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        int row = sysConfigMapper.insertConfig(config);
        if (row > 0) {
            redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
        }
        return row;
    }

    /**
     * 修改参数配置
     *
     * @param config 参数配置信息
     * @return 结果
     */
    @Override
    public int updateConfig(SysConfig config) {
        SysConfig temp = sysConfigMapper.selectConfigById(config.getConfigId());
        if (!StringUtils.equals(temp.getConfigKey(), config.getConfigKey())) {
            redisCache.deleteObject(getCacheKey(temp.getConfigKey()));
        }
        int row = sysConfigMapper.updateConfig(config);
        if (row > 0) {
            redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
        }
        return row;
    }

    /**
     * 批量删除参数信息
     *
     * @param configIds 需要删除的参数ID
     */
    @Override
    public void deleteConfigByIds(Long[] configIds) {
        for (Long configId : configIds) {
            SysConfig config = selectConfigById(configId);
            if (StringUtils.equals(Constants.Y, config.getConfigType())) {
                throw new ServiceException(StringUtils.format("内置参数{}不能删除 ", config.getConfigKey()));
            }
            sysConfigMapper.selectConfigById(configId);
            redisCache.deleteObject(getCacheKey(config.getConfigKey()));
        }
    }
    @PostConstruct
    public void init() {
        loadConfigCache();
    }

    /**
     * 重置参数缓存数据
     */
    @Override
    public void resetConfigCache() {
        Collection<String> keys = redisCache.keys(CacheConstants.SYS_CONFIG_KEY + "*");
        redisCache.deleteObject(keys);
        loadConfigCache();
    }
    public void loadConfigCache() {
        List<SysConfig> configsList = sysConfigMapper.selectConfigList(new SysConfig());
        for (SysConfig config : configsList) {
            redisCache.setCacheObject(CacheConstants.SYS_CONFIG_KEY + config.getConfigKey(), config.getConfigValue());
        }
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey) {
        return CacheConstants.SYS_CONFIG_KEY + configKey;
    }
}
